using WindowsLive.Writer.Api;
using System.Windows.Forms;

namespace TimHeuer.WindowsLiveWriter.S34Writer
{
    [InsertableContentSource("S3 Object")]
    [WriterPlugin("C5223B0A-1CEC-4cb6-8F5B-EB71B1EA9CDD", "S3 Object", "Images.S3.png", 
         PublisherUrl = "http://www.codeplex.com/s3browser", Description = "Plugin for browsing and inserting Amazon S3 object references.", HasEditableOptions=true)]
    public class S3ContentSource : ContentSource
    {
        public override DialogResult CreateContent(IWin32Window dialogOwner, ref string content)
        {
            S3Context context = new S3Context(base.Options);
            if (!context.IsValid)
            {
                EditOptions(dialogOwner);
            }

            using (S3Browser s3b = new S3Browser())
            {
                s3b.SetContext(context);
                DialogResult formResult = s3b.ShowDialog(dialogOwner);
                if (formResult == DialogResult.OK)
                {
                    content = s3b.HtmlFragment;
                }
                return formResult;
            }
        }

        public override void EditOptions(IWin32Window dialogOwner)
        {
            using (S3Settings s3s = new S3Settings(new S3Context(base.Options)))
            {
                if (s3s.ShowDialog(dialogOwner) == DialogResult.OK)
                {
                    S3Context ctx = new S3Context(base.Options);
                    ctx.AccessKeyId = s3s.AccessKeyId;
                    ctx.SecretAccessKey = s3s.SecretAccessKey;
                    ctx.AutoUploadWithFileName = s3s.AutoUploadWithFileName;
                }
            }
        }
    }
}
